# -*- coding:utf-8 -*-
import xlrd
import xlwt
from pyExcelerator import *
from xlutils.copy import copy
from openpyxl import load_workbook
import re
import sys
reload(sys)
sys.setdefaultencoding('utf8') 
filecs="HexArray.cs"
filenamecs= open(filecs,'a')

def hex2arr( hexfile1, hexfile2): 
       
    hex1= open(hexfile1,'r')
    hex2= open(hexfile2,'r')
   
    seg_no = 0
    seg_size = 3
    flashAddr = [0xC40000,0xC4A000,0xC06000]   
    FileSize  = [0x003000,0x001000,0x01A000]
    flash_checksum = [0, 0, 0]
    print>>filenamecs, "namespace WinCANTool"
    print>>filenamecs, "{"
    print>>filenamecs, "    public partial class FlashWindow1319"  
    print>>filenamecs, "    {"    
    print>>filenamecs, "        public byte[][] SegData;"
    print>>filenamecs, "        public int[] FileSize;"   
    print>>filenamecs, "        public int[] flashAddr;" 
    print>>filenamecs, "        public ushort[] flash_checksum;" 
    print>>filenamecs, "        public const int seg_max = %d;" %(seg_size)
    print>>filenamecs, "        public void InitSegData(  )" 
    print>>filenamecs, "        {"
    print>>filenamecs, "            FileSize = new int[%d];" %(seg_size)
    print>>filenamecs, "            FileSize[0] = 0x%x;"  %(FileSize[0])
    print>>filenamecs, "            FileSize[1] = 0x%x;"  %(FileSize[1])
    print>>filenamecs, "            FileSize[2] = 0x%x;"  %(FileSize[2])
    
    print>>filenamecs, "            flash_checksum = new ushort[%d];" %(seg_size)
    
    print>>filenamecs, "            flashAddr = new int[%d];" %(seg_size)
    print>>filenamecs, "            flashAddr[0] = 0x%x;" %(flashAddr[0])
    print>>filenamecs, "            flashAddr[1] = 0x%x;" %(flashAddr[1])
    print>>filenamecs, "            flashAddr[2] = 0x%x;" %(flashAddr[2])
    
    print>>filenamecs, "            SegData = new byte[%d][];"  %(seg_size)
    print>>filenamecs, "            SegData[%d] = new byte[0x%x]" %(seg_no, FileSize[seg_no])
    print>>filenamecs, "            {"      
  
    seg_no = 0
    
    for LineData in hex1:

       index = 1
       len = int(LineData[index:index+2],16)
       
       index += 2
       addr = int(LineData[index:index+4],16)
       
       index += 4
       section = int(LineData[index:index+2],16)
       
       index += 2
       
       if (section == 0x00):
           if (seg_no < 1):
               if (addr >= (flashAddr[seg_no+1] & 0xffff)):            
                   print>>filenamecs, "            }; // seg_no=%d, startaddr=0x%x, size=0x%x" % (seg_no, flashAddr[seg_no], FileSize[seg_no])  
                   seg_no += 1
                   #FileSize[seg_no] = 0
                   print>>filenamecs, "            SegData[%d] = new byte[0x%x]" %(seg_no, FileSize[seg_no])
                   print>>filenamecs, "            {"
           hex_data = ''
           for l in range(0, len):
               hex_data += "0x" +LineData[index:index+2]+','
               flash_checksum[seg_no] += int(LineData[index:index+2],16);
               #FileSize[seg_no] += 1
               index += 2
               
           print>>filenamecs, "                %s"       % (hex_data)    
              
    print>>filenamecs, "            }; // seg_no=%d, startaddr=0x%x, size=0x%x" % (seg_no, flashAddr[seg_no], FileSize[seg_no])              
     
    seg_no = 2
    print>>filenamecs, "            SegData[%d] = new byte[0x%x]" %(seg_no, FileSize[seg_no])
    print>>filenamecs, "            {"      

    for LineData in hex2:

        index = 1
        len = int(LineData[index:index+2],16)
        
        index += 2
        addr = int(LineData[index:index+4],16)
        
        index += 4
        section = int(LineData[index:index+2],16)
        
        index += 2
        
        if (section == 0x00):
            hex_data = ''
            for l in range(0, len):
                hex_data += "0x" +LineData[index:index+2]+','
                flash_checksum[seg_no] += int(LineData[index:index+2],16);
                #FileSize[seg_no] += 1
                index += 2
                
            print>>filenamecs, "                %s"       % (hex_data)                  
    print>>filenamecs, "            }; // seg_no=%d, startaddr=0x%x, size=0x%x" % (seg_no, flashAddr[seg_no], FileSize[seg_no])    
    
   
            

    for i in range(0,seg_size):
        print>>filenamecs, "            flash_checksum[%d] = 0x%x;" %(i, flash_checksum[i] & 0xffff)
    
    print>>filenamecs, "        }"

    print>>filenamecs, "    }"
    print>>filenamecs, "}"
if __name__ == '__main__':

    filenamecs= open(filecs,'w')
    filenamecs = open(filecs,'a');    
    hex2arr("221529274548.hex","221529275533.hex")


    
    
    
